package com.example.myunisdkdemo;

import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.app.Activity;

/**
 * UIConfigurator 类用于配置应用的用户界面设置，如透明状态栏和导航栏。
 */
public class UIConfigurator {
    private Activity activity;

    /**
     * 构造函数
     * @param activity 调用活动的上下文
     */
    public UIConfigurator(Activity activity) {
        this.activity = activity;
    }

    /**
     * 设置透明状态栏和导航栏。
     */
    public void setupTransparentStatusBar() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT); // 设置状态栏颜色
            window.setNavigationBarColor(Color.TRANSPARENT); // 设置导航栏颜色
        } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            setTranslucentNavigation(true); // 设置透明导航栏
        }
    }

    /**
     * 设置导航栏是否透明。
     * @param on 是否开启透明导航栏
     */
    private void setTranslucentNavigation(boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

        if (on) {
            winParams.flags |= bits; // 开启导航栏透明
        } else {
            winParams.flags &= ~bits; // 关闭导航栏透明
        }
        win.setAttributes(winParams);
    }
}